﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using TCS.DBUtility;//请先添加引用
namespace TCS.DAL
{
    /// <summary>
    /// 数据访问类:MemberInfo
    /// </summary>
    public partial class MemberInfo
    {
        public MemberInfo()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from MemberInfo");
            strSql.Append(" where ");
            SqlParameter[] parameters = {
};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MemberID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from MemberInfo");
            strSql.Append(" where Id=@MemberID ");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberID", SqlDbType.Int,4)};
            parameters[0].Value = MemberID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 验证登陆，成功返回True
        /// </summary>
        /// <param name="strName">用户名</param>
        /// <param name="strPassword">密码</param>
        /// <returns></returns>
        public bool Exists(string strName, string strPassword)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from MemberInfo");
            strSql.Append(" where MI_username=@MI_username and MI_password=@MI_password");
            SqlParameter[] parameters = {
					new SqlParameter("@MI_username", SqlDbType.VarChar,50),
                    new SqlParameter("@MI_password", SqlDbType.VarChar,50)                   
                                        };
            parameters[0].Value = strName;
            parameters[1].Value = strPassword;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(TCS.Model.MemberInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into MemberInfo(");
            strSql.Append("MI_username,MI_password,MI_Email,MI_TrueName,MemberType,MI_phone,Company,VIPPrice,AddDate,startDate,endDate,isEnable,verify)");
            strSql.Append(" values (");
            strSql.Append("@MI_username,@MI_password,@MI_Email,@MI_TrueName,@MemberType,@MI_phone,@Company,@VIPPrice,@AddDate,@startDate,@endDate,@isEnable,@verify)");
            strSql.Append(";select IDENT_CURRENT('MemberInfo')");
            SqlParameter[] parameters = {
					new SqlParameter("@MI_username", SqlDbType.VarChar,50),
					new SqlParameter("@MI_password", SqlDbType.VarChar,50),
					new SqlParameter("@MI_Email", SqlDbType.VarChar,50),
					new SqlParameter("@MI_TrueName", SqlDbType.VarChar,50),
					new SqlParameter("@MemberType", SqlDbType.Int,4),
					new SqlParameter("@MI_phone", SqlDbType.VarChar,30),
					new SqlParameter("@Company", SqlDbType.VarChar,200),
					new SqlParameter("@VIPPrice", SqlDbType.Money,8),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@startDate", SqlDbType.DateTime),
					new SqlParameter("@endDate", SqlDbType.DateTime),
					new SqlParameter("@isEnable", SqlDbType.Bit,1),
					new SqlParameter("@verify", SqlDbType.Int,4)};
            parameters[0].Value = model.MI_username;
            parameters[1].Value = model.MI_password;
            parameters[2].Value = model.MI_Email;
            parameters[3].Value = model.MI_TrueName;
            parameters[4].Value = model.MemberType;
            parameters[5].Value = model.MI_phone;
            parameters[6].Value = model.Company;
            parameters[7].Value = model.VIPPrice;
            parameters[8].Value = model.AddDate;
            parameters[9].Value = model.startDate;
            parameters[10].Value = model.endDate;
            parameters[11].Value = model.isEnable;
            parameters[12].Value = model.verify;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TCS.Model.MemberInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update MemberInfo set ");
            strSql.Append("MI_username=@MI_username,");
            strSql.Append("MI_password=@MI_password,");
            strSql.Append("MI_Email=@MI_Email,");
            strSql.Append("MI_TrueName=@MI_TrueName,");
            strSql.Append("MemberType=@MemberType,");
            strSql.Append("MI_phone=@MI_phone,");
            strSql.Append("Company=@Company,");
            strSql.Append("VIPPrice=@VIPPrice,");
            strSql.Append("AddDate=@AddDate,");
            strSql.Append("startDate=@startDate,");
            strSql.Append("endDate=@endDate,");
            strSql.Append("isEnable=@isEnable,");
            strSql.Append("verify=@verify");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@MI_username", SqlDbType.VarChar,50),
					new SqlParameter("@MI_password", SqlDbType.VarChar,50),
					new SqlParameter("@MI_Email", SqlDbType.VarChar,50),
					new SqlParameter("@MI_TrueName", SqlDbType.VarChar,50),
					new SqlParameter("@MemberType", SqlDbType.Int,4),
					new SqlParameter("@MI_phone", SqlDbType.VarChar,30),
					new SqlParameter("@Company", SqlDbType.VarChar,200),
					new SqlParameter("@VIPPrice", SqlDbType.Money,8),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@startDate", SqlDbType.DateTime),
					new SqlParameter("@endDate", SqlDbType.DateTime),
					new SqlParameter("@isEnable", SqlDbType.Bit,1),
					new SqlParameter("@verify", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.MI_username;
            parameters[1].Value = model.MI_password;
            parameters[2].Value = model.MI_Email;
            parameters[3].Value = model.MI_TrueName;
            parameters[4].Value = model.MemberType;
            parameters[5].Value = model.MI_phone;
            parameters[6].Value = model.Company;
            parameters[7].Value = model.VIPPrice;
            parameters[8].Value = model.AddDate;
            parameters[9].Value = model.startDate;
            parameters[10].Value = model.endDate;
            parameters[11].Value = model.isEnable;
            parameters[12].Value = model.verify;
            parameters[13].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MemberInfo ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MemberInfo ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="Id">用户ID</param>
        /// <returns></returns>
        public TCS.Model.MemberInfo GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,MI_username,MI_password,MI_Email,MI_TrueName,MemberType,MI_phone,Company,VIPPrice,AddDate,startDate,endDate,isEnable,verify from MemberInfo ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            TCS.Model.MemberInfo model = new TCS.Model.MemberInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.MI_username = ds.Tables[0].Rows[0]["MI_username"].ToString();
                model.MI_password = ds.Tables[0].Rows[0]["MI_password"].ToString();
                model.MI_Email = ds.Tables[0].Rows[0]["MI_Email"].ToString();
                model.MI_TrueName = ds.Tables[0].Rows[0]["MI_TrueName"].ToString();
                if (ds.Tables[0].Rows[0]["MemberType"].ToString() != "")
                {
                    model.MemberType = int.Parse(ds.Tables[0].Rows[0]["MemberType"].ToString());
                }
                model.MI_phone = ds.Tables[0].Rows[0]["MI_phone"].ToString();
                model.Company = ds.Tables[0].Rows[0]["Company"].ToString();
                if (ds.Tables[0].Rows[0]["VIPPrice"].ToString() != "")
                {
                    model.VIPPrice = decimal.Parse(ds.Tables[0].Rows[0]["VIPPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["startDate"].ToString() != "")
                {
                    model.startDate = DateTime.Parse(ds.Tables[0].Rows[0]["startDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["endDate"].ToString() != "")
                {
                    model.endDate = DateTime.Parse(ds.Tables[0].Rows[0]["endDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isEnable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isEnable"].ToString() == "1") || (ds.Tables[0].Rows[0]["isEnable"].ToString().ToLower() == "true"))
                    {
                        model.isEnable = true;
                    }
                    else
                    {
                        model.isEnable = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["verify"].ToString() != "")
                {
                    model.verify = int.Parse(ds.Tables[0].Rows[0]["verify"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 根据用户名得到一个实体
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns></returns>
        public TCS.Model.MemberInfo GetModel(string name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,MI_username,MI_password,MI_Email,MI_TrueName,MemberType,MI_phone,Company,VIPPrice,AddDate,startDate,endDate,isEnable,verify from MemberInfo ");
            strSql.Append(" where MI_username=@MI_username");
            SqlParameter[] parameters = {
					new SqlParameter("@MI_username", SqlDbType.VarChar,50)
};
            parameters[0].Value = name;

            TCS.Model.MemberInfo model = new TCS.Model.MemberInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.MI_username = ds.Tables[0].Rows[0]["MI_username"].ToString();
                model.MI_password = ds.Tables[0].Rows[0]["MI_password"].ToString();
                model.MI_Email = ds.Tables[0].Rows[0]["MI_Email"].ToString();
                model.MI_TrueName = ds.Tables[0].Rows[0]["MI_TrueName"].ToString();
                if (ds.Tables[0].Rows[0]["MemberType"].ToString() != "")
                {
                    model.MemberType = int.Parse(ds.Tables[0].Rows[0]["MemberType"].ToString());
                }
                model.MI_phone = ds.Tables[0].Rows[0]["MI_phone"].ToString();
                model.Company = ds.Tables[0].Rows[0]["Company"].ToString();
                if (ds.Tables[0].Rows[0]["VIPPrice"].ToString() != "")
                {
                    model.VIPPrice = decimal.Parse(ds.Tables[0].Rows[0]["VIPPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["startDate"].ToString() != "")
                {
                    model.startDate = DateTime.Parse(ds.Tables[0].Rows[0]["startDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["endDate"].ToString() != "")
                {
                    model.endDate = DateTime.Parse(ds.Tables[0].Rows[0]["endDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isEnable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isEnable"].ToString() == "1") || (ds.Tables[0].Rows[0]["isEnable"].ToString().ToLower() == "true"))
                    {
                        model.isEnable = true;
                    }
                    else
                    {
                        model.isEnable = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["verify"].ToString() != "")
                {
                    model.verify = int.Parse(ds.Tables[0].Rows[0]["verify"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,MI_username,MI_password,MI_Email,MI_TrueName,MemberType,MI_phone,Company,VIPPrice,AddDate,startDate,endDate,isEnable,verify ");
            strSql.Append(" FROM MemberInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,MI_username,MI_password,MI_Email,MI_TrueName,MemberType,MI_phone,Company,VIPPrice,AddDate,startDate,endDate,isEnable,verify ");
            strSql.Append(" FROM MemberInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "MemberInfo";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="pwd">新密码</param>
        public void UpdatePassword(int id, string pwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update MemberInfo set MI_password=@MI_password");
            strSql.Append(" where  Id=@MemberID ");


            SqlParameter[] parameters = {
					new SqlParameter("@MemberID", SqlDbType.Int,4),
                    new SqlParameter("@MI_password", SqlDbType.VarChar,50)                    };
            parameters[0].Value = id;
            parameters[1].Value = pwd;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        #endregion  Method
    }
}

